def findExtremum(root, seek_max=True):
    # 如果树为空，根据是否寻找最大值返回负无穷大或正无穷大
    if root is None:
        return float('-inf') if seek_max else float('inf')

    # 递归地找到左子树和右子树的极值
    left_extremum = findExtremum(root.left, seek_max)
    right_extremum = findExtremum(root.right, seek_max)

    # 根据是否寻找最大值，返回当前节点值、左子树极值和右子树极值中的最大者或最小者
    if seek_max:
        return max([root.val, left_extremum, right_extremum])
    else:
        return min([root.val, left_extremum, right_extremum])


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


# 构建示例树
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.right.left = TreeNode(5)
root.right.right = TreeNode(6)
root.left.left.right = TreeNode(7)
root.right.left.left = TreeNode(8)
root.right.right.right = TreeNode(9)

# 找到并输出树中的最大值
max_value = findExtremum(root)
print("二叉树中的最大值:", max_value)